package org.moka.ioio.ioiopilot.input;

import org.moka.ioio.ioiopilot.IOIOThread;
import org.moka.ioio.ioiopilot.gui.GraphViewThread;

import android.util.Log;
import ioio.lib.api.IOIO;
import ioio.lib.api.PulseInput;
import ioio.lib.api.exception.ConnectionLostException;

public class RxInputs extends AbstractInput implements InputInterface{
 
	// INPUT PINS 
	protected static final int PIN_IN_THROTTLE = 3;
	protected static final int PIN_IN_ELEVATOR = 4;
	protected static final int PIN_IN_AILERON = 5;
	protected static final int PIN_IN_RUDDER = 6;
	
	// INPUTS
	public static final int NUM_CHANNELS = 4;
	public static final int THROTTLE = 0;
	public static final int ELEVATOR = 1;
	public static final int AILERON = 2;
	public static final int RUDDER = 3;	
	
	protected static final int MIN_PULSE_MS = 900;	// Min pulse length (us)
	public static final long DELAY_RX_SIGNAL = 60; 	// Read every 60ms
	
	protected PulseInput[] input = new PulseInput[NUM_CHANNELS] ;
	private int count;
	
	private ReadThread[] readThread = new ReadThread[NUM_CHANNELS];

	private boolean READY = false;
	
	public RxInputs(IOIO ioio) throws ConnectionLostException {
		super(NUM_CHANNELS);
		
		// INIT INPUT
		input[THROTTLE] = ioio.openPulseInput(PIN_IN_THROTTLE , PulseInput.PulseMode.POSITIVE);
		input[ELEVATOR] = ioio.openPulseInput(PIN_IN_ELEVATOR , PulseInput.PulseMode.POSITIVE);
		input[AILERON] = ioio.openPulseInput(PIN_IN_AILERON , PulseInput.PulseMode.POSITIVE);
		// input[RUDDER] = ioio.openPulseInput(PIN_IN_RUDDER , PulseInput.PulseMode.POSITIVE);
		
		/* CREATE READ THREADS FOR EACH CHANNEL */
		for(int n=0; n<NUM_CHANNELS; n++)
		{
			readThread[n] = new ReadThread(n);
		}
		READY  = true;
		
		Log.v(IOIOThread.TAG, "RxInputs.read()");
		for(int n=0; n<NUM_CHANNELS; n++)
		{
			if (!readThread[n].isAlive()) {
				readThread[n] = new ReadThread(n);
				readThread[n].start();
			}
		}
	}
	
	

	class ReadThread extends Thread {

		private boolean READING = false;
		private int myChannel = 0;
		private boolean RUN = true;

		public ReadThread(int channel)
		{
			myChannel = channel;
		}
		@Override
		public void run()
		{
			while(RUN)
			{
				READING = true;
				try {
						if(input[myChannel]!=null)
						{
							value[myChannel] = (int) (( input[myChannel].getDuration()*1000000 ) - MIN_PULSE_MS) ;
							printLog();
						}else
						{
							//Log.w(IOIOThread.TAG,"ReadThread: channel "+myChannel+" is null");
						}
						
				} catch (InterruptedException e) {
					Log.e(IOIOThread.TAG,"readTHread: "+e.getMessage());
					RUN = false;
				} catch (ConnectionLostException e) {
					Log.w(IOIOThread.TAG,"ReadThread: "+e.getMessage());
					RUN = false;
				}
				READING = false;
				try {
					sleep(DELAY_RX_SIGNAL);
				} catch (InterruptedException e) {
					Log.e(IOIOThread.TAG,"readTHread: "+e.getMessage());
					RUN = false;
				}
			}
			
			
		}
		
		private void printLog() {
			if(count>50)
			{				
				Log.v(IOIOThread.TAG,					
					"Rx input pulse THROTTLE: " + value[THROTTLE] + " ms "+ 
					"ELEVATOR: " + value[ELEVATOR] + " ms " + 
					"AILERON: " + value[AILERON] + " ms "+
					"RUDDER: " + value[RUDDER] + " ms " );
				Log.d(IOIOThread.TAG,					
						"Rx input values THROTTLE: " + value[THROTTLE] + " V "+ 
						"ELEVATOR: " + value[ELEVATOR] + " V " + 
						"AILERON: " + value[AILERON] + " V "+
						"RUDDER: " + value[RUDDER] + " V " );
				count = 0;
			}			
			count++;
			
		}
		
		
	}
	

	
}
